#!/usr/bin/perl

use English;

sub usage {
	print STDERR "usage: $PROGRAM_NAME ini-file\n";
}

if ($#ARGV != 0) {
	usage();
	exit 1;
}

$infile = $ARGV[0];

if ($infile !~ /\.ini$/) {
	print STDERR "$infile does not end with .ini\n";
	exit 2;
}

$outfile = $infile;
$outfile =~ s/.ini$/.xml/;

open(IN ,"<$infile")  or die "cannot open $infile";
open(OUT,">$outfile") or die "cannot open $outfile";

print OUT "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
print OUT "<sections>\n";

$insection = 0;

while ($line = <IN>) {
	chomp $line;
	if ($line =~ /^;(.*)/) {
		if ($insection) {
			print OUT "\t";
		}
		print OUT "\t<comment>$1</comment>\n";
	}
	elsif ($line =~ /^\[(.*)\]$/) {
		if ($insection) {
			print OUT "\t</section>\n";
		}
		print OUT "\t<section name=\"$1\">\n";
		$insection = 1;
	}
	elsif ($line =~ /(.*)=(.*)/) {
		print OUT "\t\t<item key=\"$1\" value=\"$2\" />\n";
	}
	else {
		print STDERR "line ignored: $line\n";
	}
}

print OUT "\t</section>\n";
print OUT "</sections>\n";

close IN;
close OUT;
